/*
package com.seaboxdata.scheduler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.BatchBasicVo;
import com.seaboxdata.entity.BatchInterfaceConfig;
import com.seaboxdata.entity.FieldInfo;
import com.seaboxdata.service.BatchInterfaceConfigService;
import com.seaboxdata.service.BatchInterfaceService;
import com.seaboxdata.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

@Component
@Configuration
@Slf4j
public class BigDataBatchInterfaceJob {

    public String getAppkey() {
        return appkey;
    }

    public String getPageSize() {
        return pageSize;
    }

    @Value("${batch.appkey}")
    private String appkey;

    @Value("${batch.pageSize}")
    private String pageSize;

    @Autowired
    private BatchInterfaceService batchInterfaceService;

    @Autowired
    private BatchInterfaceConfigService batchInterfaceConfigService;

    public static final String DEFAULT_PAGESIZE_KEY = "@pageSize";

    public static final String DEFAULT_PAGENUM_KEY = "@pageNo";

    public static final String DEFAULT_ETL_DATE_KEY = "etl_date";

    public static final String DEFAULT_ACCESS_TOKEN_KEY = "access_token";

    public static final String DEFAULT_CONTENT_TYPE_KEY = "Content-Type";

    public static final String DEFAULT_CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded";

    @Scheduled(cron = "${bigData.batch.interface.interval}")
    public void doCollect() {
        log.info("batch interface begin excute");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //未初始化的先要进行表的初始化
        List<BatchInterfaceConfig> batchInterfaceNotInitConfigs =
                batchInterfaceConfigService.getAllByStatusAndIsInit("0", "0");

        if (!CollectionUtils.isEmpty(batchInterfaceNotInitConfigs)) {
            for (BatchInterfaceConfig batchInterfaceNotInitConfig : batchInterfaceNotInitConfigs) {
                List<FieldInfo> fieldInfos = new ArrayList<>();
                StringBuilder sb = new StringBuilder();
                //组装建表语句SQL
                buildCreateSql(sb, batchInterfaceNotInitConfig, fieldInfos);
                try {
                    batchInterfaceService.createTable(new String(sb));
                    batchInterfaceConfigService.deleteAll(batchInterfaceNotInitConfig);
                } catch (SQLException e) {
                    e.printStackTrace();
                    log.error("create table " + batchInterfaceNotInitConfig.getTableName() + " error");
                    continue;
                }

                //第一次调用,获取所有的数据
                Map<String, String> paramsMap = new HashMap<>();
                paramsMap.put(DEFAULT_ACCESS_TOKEN_KEY, appkey);
                paramsMap.put(DEFAULT_PAGESIZE_KEY, "1");
                paramsMap.put(DEFAULT_PAGENUM_KEY, "1");
                Map<String, String> headerMap = new HashMap<>();
                headerMap.put(DEFAULT_CONTENT_TYPE_KEY, DEFAULT_CONTENT_TYPE_VALUE);

                //计算循环调用
                try {
                    String result = HttpUtils.post(batchInterfaceNotInitConfig.getUrl(), paramsMap, headerMap);
                    BatchBasicVo batchBasicVo = JSON.parseObject(result, BatchBasicVo.class);
                    if (batchBasicVo.getResultcode() == 200) {
                        Integer total = batchBasicVo.getTotal();
                        Integer count = Integer.valueOf(pageSize);
                        int excuteCount = 1;
                        if (count == total) {
                            excuteCount = 1;
                        } else {
                            excuteCount = total % count == 0 ? total / count : total / count + 1;
                        }

                        if (batchBasicVo.getRecords().size() == 0) {
                            batchInterfaceConfigService.updateIncresementColumnTableName(batchInterfaceNotInitConfig.getTableName(), "noData");
                        } else {
                            if (batchBasicVo.getRecords().getObject(0, JSONObject.class).get("etl_date") == null) {
                                if (batchBasicVo.getRecords().getObject(0, JSONObject.class).get("ETL_DATE") != null) {
                                    batchInterfaceConfigService.updateIncresementColumnTableName(batchInterfaceNotInitConfig.getTableName(), "ETL_DATE");
                                } else {
                                    batchInterfaceConfigService.updateIncresementColumnTableName(batchInterfaceNotInitConfig.getTableName(), "undefined");
                                }
                            } else {
                                batchInterfaceConfigService.updateIncresementColumnTableName(batchInterfaceNotInitConfig.getTableName(), "etl_date");
                            }
                        }

                        //开启多线程访问,并且将结果入库
                        ExecutorService executorService = Executors.newFixedThreadPool(100);
                        for (int i = 1; i <= excuteCount % 100 + 1; i++) {
                            if (i == excuteCount % 100 + 1) {
                                executorService.execute(new BigDataRunnable((i - 1) * 10000 + 1, i * 10000, Integer.valueOf(pageSize), this, batchInterfaceNotInitConfig));
                            } else {
                                executorService.execute(new BigDataRunnable((i - 1) * 10000 + 1, excuteCount, Integer.valueOf(pageSize), this, batchInterfaceNotInitConfig));
                            }
                        }


                        paramsMap.put(DEFAULT_PAGESIZE_KEY, pageSize);
                        try {
                            for (int i = 1; i <= excuteCount; i++) {
                                paramsMap.put(DEFAULT_PAGENUM_KEY, i + "");
                                handler(batchInterfaceNotInitConfig, paramsMap, headerMap, fieldInfos);
                                Thread.sleep(50);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                        //获取目前数据库的总数据量
                        int totalCount = batchInterfaceConfigService.getTotalCount(batchInterfaceNotInitConfig);
                        if (totalCount != total) {
                            log.error("init table data is not same,tableName: " + batchInterfaceNotInitConfig.getTableName());
                            continue;
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }

                //插入数据完成,修改状态为初始化完成
                batchInterfaceConfigService.updateByTableName(batchInterfaceNotInitConfig.getTableName());
            }
            return;
        }
        log.info("batchInterface excute end");
    }

    public Integer handler(BatchInterfaceConfig batchInterfaceNotInitConfig, Map<String, String> paramsMap, Map<String, String> headerMap, List<FieldInfo> fieldInfos) throws Exception {
        String result = HttpUtils.post(batchInterfaceNotInitConfig.getUrl(), paramsMap, headerMap);
        if (!StringUtils.isEmpty(result)) {
            BatchBasicVo batchBasicVo = JSON.parseObject(result, BatchBasicVo.class);
            if (batchBasicVo.getResultcode() == 200) {
                JSONArray records = batchBasicVo.getRecords();
                Iterator<Object> iterator = records.iterator();
                //拼接批量插入的表头
                String pre = "insert into " + batchInterfaceNotInitConfig.getTableName() + " (";
                for (int i = 0; i < fieldInfos.size(); i++) {
                    if (i < fieldInfos.size() - 1) {
                        pre = pre + fieldInfos.get(i).getFieldName() + ",";
                    } else {
                        pre = pre + fieldInfos.get(i).getFieldName() + ") values ";
                    }
                }
                StringBuilder valueBuilder = new StringBuilder();
                while (iterator.hasNext()) {
                    JSONObject jsonResult = (JSONObject) iterator.next();
                    if (jsonResult.size() != fieldInfos.size() && jsonResult.size() > fieldInfos.size()) {
                        List<String> s = new ArrayList<>();
                        for (Map.Entry<String, Object> stringObjectEntry : jsonResult.entrySet()) {
                            if (!s.contains(stringObjectEntry.getKey().toLowerCase())) {
                                s.add(stringObjectEntry.getKey().toLowerCase());
                            }
                        }

                        for (FieldInfo fieldInfo : fieldInfos) {
                            if (!s.contains(fieldInfo.getFieldName())) {
                                System.out.println(fieldInfo.getFieldName());
                            }
                        }
                    } else {
                        //拼接SQL
                        for (int i = 0; i < fieldInfos.size(); i++) {
                            FieldInfo currentField = fieldInfos.get(i);
                            Object value = null;
                            if ("etl_date".equals(currentField.getFieldName())) {
                                value = jsonResult.get(currentField.getFieldName());
                                if (value == null) {
                                    value = jsonResult.get(currentField.getFieldName().toUpperCase());
                                }
                            } else {
                                value = jsonResult.get(currentField.getFieldName());
                                if (value == null) {
                                    value = jsonResult.get(currentField.getFieldName().toUpperCase());
                                }
                            }

                            if (value instanceof String) {
                                String valueStr = (String) value;
                                if (StringUtils.isEmpty(valueStr)) {
                                    valueStr = null;
                                } else {
                                    if (valueStr.contains("\\")) {
                                        valueStr = valueStr.replaceAll("\\\\", "");
                                    }
                                    if (valueStr.contains("'")) {
                                        valueStr = valueStr.replaceAll("'", "");
                                    }
                                    if (valueStr.contains("\"")) {
                                        valueStr = valueStr.replaceAll("\"", "");
                                    }
                                    if (StringUtils.isEmpty(valueStr)) {
                                        valueStr = null;
                                    } else {
                                        //判断是否有特殊汉字
                                        valueStr = "'" + valueStr + "'";
                                    }
                                }
                                value = valueStr;
                            }
                            currentField.setFieldValue(value);
                            if (i < fieldInfos.size() - 1) {
                                if (i == 0) {
                                    valueBuilder.append("(");
                                }
                                valueBuilder.append(currentField.getFieldValue()).append(",");
                            } else {
                                valueBuilder.append(currentField.getFieldValue()).append("),");
                            }
                        }

                    }
                }

                if (valueBuilder.length() == 0) {
                    log.info("no data can insert");
                    return batchBasicVo.getTotal();
                } else {
                    String values = valueBuilder.substring(0, valueBuilder.length() - 1);
                    String dataAndBuildInsertSql = pre + values + ";";
                    if (!StringUtils.isEmpty(dataAndBuildInsertSql)) {
                        int count = batchInterfaceService.batchInsert(dataAndBuildInsertSql);
                        return batchBasicVo.getTotal();
                    }
                }
            }
        }
        return 0;
    }

    public void buildCreateSql(StringBuilder sb, BatchInterfaceConfig batchInterfaceNotInitConfig, List<FieldInfo> fieldInfos) {
        sb.append("create table if not exists ")
                .append(batchInterfaceNotInitConfig.getTableName())
                .append("( ");
        //组装建表语句
        String[] fields = batchInterfaceNotInitConfig.getColumns().split("&");
        for (int i = 0; i < fields.length; i++) {
            String field = fields[i];
            String[] fieldInfoStr = field.split(":");
            FieldInfo fieldInfo = new FieldInfo(fieldInfoStr[0], fieldInfoStr[2], fieldInfoStr[1]);
            fieldInfos.add(fieldInfo);
            if (i == fields.length - 1) {
                sb.append(fieldInfo.getFieldName()).append(" ").append(fieldInfo.getFieldType()).append(" comment '").append(fieldInfo.getFieldChineseName()).append("'")
                        .append(") ").append("CHARACTER set utf8mb4 collate utf8mb4_general_ci").append(" comment '").append(batchInterfaceNotInitConfig.getTableChineseName()).append("'");
            } else {
                sb.append(fieldInfo.getFieldName()).append(" ").append(fieldInfo.getFieldType()).append(" comment '").append(fieldInfo.getFieldChineseName()).append("'").append(",");
            }
        }
    }

    public List<FieldInfo> getFieldInfos(BatchInterfaceConfig batchInterfaceNotInitConfig) {
        List<FieldInfo> fieldInfos = new ArrayList<>();
        String[] fields = batchInterfaceNotInitConfig.getColumns().split("&");
        for (int i = 0; i < fields.length; i++) {
            String field = fields[i];
            String[] fieldInfoStr = field.split(":");
            FieldInfo fieldInfo = new FieldInfo(fieldInfoStr[0], fieldInfoStr[2], fieldInfoStr[1]);
            fieldInfos.add(fieldInfo);

        }
        return fieldInfos;
    }

    public static boolean isSpecialChar(int n) {
        if (!(19968 <= n && n <= 40869) && !(n >= 48 && n <= 57) && !(n >= 65 && n <= 90)) {
            return true;
        }
        return false;
    }

    public static String handler(String s) {
        int n = 0;
        Map<Integer, Integer> maps = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            int start = 0;
            int end = 0;
            n = (int) s.charAt(i);
            int step = 0;
            if (isSpecialChar(n)) {
                while (isSpecialChar(n)) {
                    start = i;
                    step++;
                    if (start + step < s.length()) {
                        end = start + step;
                        try {
                            n = (int) s.charAt(end);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                i = i + step;
                maps.put(start, end);
            }
        }
        for (Map.Entry<Integer, Integer> integerIntegerEntry : maps.entrySet()) {
            String specialChar = s.substring(integerIntegerEntry.getKey(), integerIntegerEntry.getValue());
            Pattern pattern = Pattern.compile("[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]");
            if (pattern == null) {
                s = s.replaceAll(specialChar, "?");
            }
        }
        return s;
    }

}
*/
